package com.example.demo.controller;

import com.example.demo.Client.Client;
import com.example.demo.Client.Send;
import com.example.demo.HelloApplication;
import com.example.demo.pojo.Friend;
import com.example.demo.pojo.Msg;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;

public class ClientController {
    private Client client;
    private Send send;
    @FXML
    public Label username;
    @FXML
    private TableView<Friend> friendList;
    @FXML
    private Label friendName;


    @FXML
    private TableView<Msg> commit;
    @FXML
    private TextArea sendMsg;
    public ObservableList<Msg> data = FXCollections.observableArrayList();
    public ObservableList<Friend> data1 = FXCollections.observableArrayList();

    public TableView<Friend> getFriendList() {
        return friendList;
    }

    public TableView<Msg> getCommit() {
        return commit;
    }
    public Label getFriendName() {
        return friendName;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public void setSend(Send send) {
        this.send = send;
    }
    public void startSend(){
        this.send.start();
    }
    @FXML
    public void sendTo() throws IOException {
        String friendID =friendName.getText();
        if(friendID !=null){
            String portString = friendID.substring(friendID.indexOf("(")+1,friendID.indexOf(")"));
            int port = Integer.parseInt(portString);
            String name= friendID.substring(0,friendID.indexOf("("));
            String msg = sendMsg.getText();
            data.add(new Msg("You say to "+name+":"+msg));
            send.sendMsg(msg,username.getText(),port);
            commit.setItems(data);
            sendMsg.clear();
        }else{
            Warning("请选择一个用户");
        }

    }
    @FXML
    public void flushList(ActionEvent actionEvent) throws IOException {
        send.sendFlush();
        ClickRow();
    }

    public void ClickRow() {
        friendList.setRowFactory( tv -> {
            TableRow<Friend> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 1 && (! row.isEmpty()) ) {
                    Friend rowData = row.getItem();
                    String username = rowData.getUsername();
                    int port = rowData.getPort();
                    friendName.setText(username+"("+port+")");
                }
            });
            return row ;
        });
    }
    public void Warning(String warning){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Warning Dialog");
        alert.setHeaderText("WARNING");
        alert.setContentText(warning);

        alert.showAndWait();
    }

}
